package sorting;
//https://leetcode.cn/leetbook/read/illustration-of-algorithm/phn3m1/
public class 冒泡排序 {
    void bubbleSort(int[] nums) {
        int N = nums.length;
        for (int i = 0; i < N - 1; i++) {          // 外循环
            for (int j = 0; j < N - i - 1; j++) {  // 内循环
                if (nums[j] > nums[j + 1]) {
                    // 交换 nums[j], nums[j + 1]
                    int tmp = nums[j];
                    nums[j] = nums[j + 1];
                    nums[j + 1] = tmp;
                }
            }
        }
    }

    //标志位优化
    void bubbleSort2(int[] nums) {
        for(int i = 0; i < nums.length - 1; i++){
            boolean flag = false;
            for (int j = 0; j < nums.length-i-1; j++){
                if(nums[j] > nums[j+1]){
                    int tmp = nums[j];
                    nums[j] = nums[j + 1];
                    nums[j + 1] = tmp;
                    flag = true;
                }
            }
            if(!flag) break;
        }
    }
}
